package engine.light;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;

import static org.lwjgl.opengl.GL11.*;

public class PointLight extends Light implements Comparable<PointLight> {
	
	private FloatBuffer position;
	
	private float constantAttenuation;
	private float linearAttenuation;
	private float quadraticAttenuation;
	
	private double priority;
	
	public PointLight() {
		super();
		
		position = BufferUtils.createFloatBuffer(4);
		position.put(3, 1);
		
		constantAttenuation = 1;
	}
	
	public FloatBuffer getPosition() {
		return position;
	}
	
	public void setPosition(float x, float y, float z) {
		position.put(0, x);
		position.put(1, y);
		position.put(2, z);
	}

	public float getConstantAttenuation() {
		return constantAttenuation;
	}

	public void setConstantAttenuation(float constantAttenuation) {
		this.constantAttenuation = constantAttenuation;
	}

	public float getLinearAttenuation() {
		return linearAttenuation;
	}

	public void setLinearAttenuation(float linearAttenuation) {
		this.linearAttenuation = linearAttenuation;
	}

	public float getQuadraticAttenuation() {
		return quadraticAttenuation;
	}

	public void setQuadraticAttenuation(float quadraticAttenuation) {
		this.quadraticAttenuation = quadraticAttenuation;
	}
	
	@Override
	public void apply(int lightSource) {
		super.apply(lightSource);
		
		position.rewind();
		
		glLight(lightSource, GL_POSITION, position);
		glLightf(lightSource, GL_CONSTANT_ATTENUATION, constantAttenuation);
		glLightf(lightSource, GL_LINEAR_ATTENUATION, linearAttenuation);
		glLightf(lightSource, GL_QUADRATIC_ATTENUATION, quadraticAttenuation);
	}
	
	public void setPriority(double priority) {
		this.priority = priority;
	}

	@Override
	public int compareTo(PointLight p) {
		if(priority > p.priority) {
			return 1;
		} else {
			return -1;
		}
	}
}
